import { createRouter, createWebHashHistory } from "vue-router";
import house from "./routes/house";
import community from "./routes/community";
import service from "./routes/service";
import other from "./routes/other";
import { loginCheck } from "./guard.js";

const commonRoutes = [];

const routes = [
  {
    path: "/",
    name: "Login",
    component: () => import("@/pages/login/newLogin.vue"),
    meta: {
      title: "登录页",
      nLogin: true,
    },
  },
  {
    path: "/home",
    name: "Home",
    component: () => import("@/pages/home/index.vue"),
    meta: {
      title: "首页"
    },
  },
  {
    path: "/table",
    name: "Table",
    component: () => import("@/pages/table/index"),
    meta: {
      title: "大屏工单展示",
      nLogin: true
    },
  },
  {
    path: "/needHeader",
    name: "NeedHeader",
    component: () => import("@/pages/needHeader/index.vue"),
    children: [...commonRoutes, ...house, ...community, ...other, ...service],
  }
];
const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
// 全局前置守卫
router.beforeEach((to, from, next) => {
  const isLogin = loginCheck();
  if (!to.meta.nLogin && !isLogin) {
    if(to.name == 'PointsForms') {
      const qrCode = to.params.qrCode || ''
      const corpId = to.query.corpId || ''
      next(`/?redirect=point-forms&qrCode=${qrCode}&corpId=${corpId}`)
    } else {
      next("/");
    }
  } else {
    next();
  }
});
export default router;
